using System;
using System.Reflection;
using Microsoft.SharePoint;

namespace AdvancedLookup
{
    public partial class AdvancedLookupFieldEditor
    {
        protected void CaptainFantastico(SPField spField)
        {
            Type type = spField.GetType();

            MethodInfo mi = type.GetMethod("SetFieldBoolValue", BindingFlags.NonPublic | BindingFlags.Instance);

            mi.Invoke(spField, new object[] { "CanToggleHidden", true });

            spField.Hidden = false;

            spField.ReadOnlyField = false;

            spField.Update();
        }

        protected void DeleteHiddenField(SPList spListParent, string internalName)
        {
            if (spListParent.Fields.ContainsField(internalName))
            {
                //CanToggleHidden
                SPField spField =
                    spListParent.Fields.GetFieldByInternalName(internalName);

                if (spField != null)
                {
                    CaptainFantastico(spField);

                    spListParent.Fields.Delete(spField.InternalName);
                }
            }
        }
    }
}
